<?php
include ("functions.php");

$key = "ABQIAAAApQrKFSIc2J9MYceoa6duVxTKXt4cKQlpVub2wjmmcKGrjO3izBTH5zKGBspaMkQKjbrdrWN3uNRYzQ";

$address = "http://maps.google.com/maps/geo?q=".urlencode($_POST["to"])."&oe=utf8&output=xml&key=$key&hl=".$lang["lang"];

$retezec = file_get_contents($address);
	
if (strpos($retezec, "<Status>")) {
	$sxml = new simplexml;
	$data = $sxml->xml_load_file($address);
	
	$echo = '<?xml version="1.0" encoding="utf-8" ?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xmlns:locify="http://client.locify.com/locify_ns/1.0" xml:lang="'.$lang["lang"].'" lang="'.$lang["lang"].'">
		<head>
			<title>'.$lang["found_address"].'</title>
		</head>
		<body class="list">
		<ul>';
	if ($data->Response->Status->code == 200) {
		if (count($data->Response->Placemark) > 1) {
			foreach($data->Response->Placemark as $placemark) {
				list($longitude, $latitude, $altitude) = explode("," ,$placemark->Point->coordinates);
				$echo .= '<li><a href="'.SERVICE_URL.'route.php?from='.$_POST["latitude"].','.$_POST["longitude"].'&to='.$latitude.','.$longitude.'&name='.urlencode($_POST["name"]).'&full='.$_POST["full"].'">'.$placemark->address.'</a></li>';
			}
		} else {
			list($longitude, $latitude, $altitude) = explode("," ,$data->Response->Placemark->Point->coordinates);
			header('Location: route.php?from='.$_POST["latitude"].','.$_POST["longitude"].'&to='.$latitude.','.$longitude.'&name='.urlencode($_POST["name"]).'&full='.$_POST["full"]);
		}
		
	} else {
		$echo .= $lang["address"].' '.$_POST["to"].' '.$lang["not_found"];
	}
	$echo .= '</ul>
		</body>
		</html>';
	echo $echo;
} else {
	echo '<?xml version="1.0" encoding="utf-8" ?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml" xmlns:locify="http://client.locify.com/locify_ns/1.0" xml:lang="'.$lang["lang"].'" lang="'.$lang["lang"].'">
	<head>
		<title>Error</title>
	</head>
	<body>
		<div>Please try again</div>
	</body>
	</html>';
}
?>
